<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="button" value="test" id="test">

    <input type="button" value="test2" id="test2">

    <script src="/jquery-2.0.2.min.js"></script>
    <script src="/axios.min.js"></script>
    <script>
        let token = '<%=token%>';
        console.log(token);
        axios.defaults.headers.common['Authorization'] = 'Bearer ' + token;
        // Add a request interceptor
        axios.interceptors.request.use(function (config) {
            // Do something before request is sent
            return config;
        }, function (error) {
            // Do something with request error
            return Promise.reject(error);
        });

        // Add a response interceptor
        axios.interceptors.response.use(function (response) {
            // Any status code that lie within the range of 2xx cause this function to trigger
            // Do something with response data
            console.log(response);
            return response;
        }, function (error) {
            // Any status codes that falls outside the range of 2xx cause this function to trigger
            // Do something with response error
            if (error.response.status == 401) {
                alert('登录信息已过期，请您重新登录！');
                return error.response;
            }
            return Promise.reject(error);
        });

        $(document).ready(function () {
            $('#test').click(async function () {
                let result = await axios.post('/test', {
                    firstName: 'Fred',
                    lastName: 'Flintstone'
                });
                console.log(result.data);
            });

            $('#test2').click(async function () {
                let result = await axios.post('/api_two', {
                    firstName: 'Fred',
                    lastName: 'Flintstone'
                });
                console.log(result.data);
            });
        });
    </script>
</body>

</html>